<template>
	<el-input v-model="inputValue" v-bind="$attrs" @input="astrictInputData" />
</template>

<script>
import emitter from 'element-ui/src/mixins/emitter';
export default {
	name: 'swCustomInput',
	inject: ['elForm'],
	mixins: [emitter],
	props: {
		value: [String, Number],
		type: { type: String, default: '' }
	},
	data() {
		return {
			inputValue: this.value
		};
	},
	watch: {
		value(val) {
			this.inputValue = val;
		},
		inputValue(val) {
			this.$emit('input', val);
			this.dispatch('ElFormItem', 'el.form.change', [val]);
		}
	},
	methods: {
		astrictInputData(value) {
			if (this.type === 'letter') {
				this.inputValue = value.replace(/[^a-zA-Z]/g, '');
			} else if (this.type === 'account') {
				this.inputValue = value.replace(/[^0-9a-zA-Z_@.]/g, '');
			} else if (this.type === 'number') {
				let regRex = /^\d+(\.\d*)?$|^\.\d+$/;
				let inputValue = value.replace(/[^0-9.]/g, '');
				if (!!inputValue && !regRex.test(inputValue)) {
					this.inputValue = inputValue.slice(0, -1);
				} else {
					this.inputValue = inputValue;
				}
			} else if (this.type === 'capital') {
				this.inputValue = value.replace(/[^0-9a-zA-Z_]/g, '').toUpperCase();
			} else if (this.type === 'address') {
				this.inputValue = value.replace(/[^a-zA-Z0-9\-/]/g, '');
			} else if (this.type === 'enum') {
				this.inputValue = value.replace(/[^0-9a-zA-Z]/g, '');
			}
		}
	}
};
</script>

<style scoped></style>
